home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / src / mmath.c < prev    next >
C/C++ Source or Header  |  1999-02-04  |  4KB  |  145 lines

  1. /* $Id: mmath.c,v 3.0 1998/01/31 20:59:27 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: mmath.c,v $
  26.  * Revision 3.0  1998/01/31 20:59:27  brianp
  27.  * initial rev
  28.  *
  29.  */
  30.  
  31.  
  32. #ifdef PC_HEADER
  33. #include "all.h"
  34. #else
  35. #include "GL/gl.h"
  36. #include "mmath.h"
  37. #endif
  38.  
  39.  
  40.  
  41. /*
  42.  * A High Speed, Low Precision Square Root
  43.  * by Paul Lalonde and Robert Dawson
  44.  * from "Graphics Gems", Academic Press, 1990
  45.  */
  46. #ifndef AMIGA
  47. /*
  48.  * SPARC implementation of a fast square root by table 
  49.  * lookup.
  50.  * SPARC floating point format is as follows:
  51.  *
  52.  * BIT 31       30      23      22      0
  53.  *     sign     exponent        mantissa
  54.  */
  55. static short sqrttab[0x100];    /* declare table of square roots */
  56.  
  57. static void init_sqrt(void)
  58. {
  59. #ifdef FAST_MATH
  60.    unsigned short i;
  61.    float f;
  62.    unsigned int *fi = (unsigned int *)&f;
  63.                 /* to access the bits of a float in  */
  64.                 /* C quickly we must misuse pointers */
  65.  
  66.    for(i=0; i<= 0x7f; i++) {
  67.       *fi = 0;
  68.  
  69.       /*
  70.        * Build a float with the bit pattern i as mantissa
  71.        * and an exponent of 0, stored as 127
  72.        */
  73.  
  74.       *fi = (i << 16) | (127 << 23);
  75.       f = sqrt(f);
  76.  
  77.       /*
  78.        * Take the square root then strip the first 7 bits of
  79.        * the mantissa into the table
  80.        */
  81.  
  82.       sqrttab[i] = (*fi & 0x7fffff) >> 16;
  83.  
  84.       /*
  85.        * Repeat the process, this time with an exponent of
  86.        * 1, stored as 128
  87.        */
  88.  
  89.       *fi = 0;
  90.       *fi = (i << 16) | (128 << 23);
  91.       f = sqrt(f);
  92.       sqrttab[i+0x80] = (*fi & 0x7fffff) >> 16;
  93.    }
  94. #endif /*FAST_MATH*/
  95. }
  96.  
  97. float gl_sqrt( float x )
  98. {
  99. #ifdef FAST_MATH
  100.    unsigned int *num = (unsigned int *)&x;
  101.                 /* to access the bits of a float in C
  102.                  * we must misuse pointers */
  103.                             
  104.    short e;                     /* the exponent */
  105.    if (x == 0.0F) return 0.0F;  /* check for square root of 0 */
  106.    e = (*num >> 23) - 127;      /* get the exponent - on a SPARC the */
  107.                 /* exponent is stored with 127 added */
  108.    *num &= 0x7fffff;            /* leave only the mantissa */
  109.    if (e & 0x01) *num |= 0x800000;
  110.                 /* the exponent is odd so we have to */
  111.                 /* look it up in the second half of  */
  112.                 /* the lookup table, so we set the   */
  113.                 /* high bit                                */
  114.    e >>= 1;                     /* divide the exponent by two */
  115.                 /* note that in C the shift */
  116.                 /* operators are sign preserving */
  117.                 /* for signed operands */
  118.    /* Do the table lookup, based on the quaternary mantissa,
  119.     * then reconstruct the result back into a float
  120.     */
  121.    *num = ((sqrttab[*num >> 16]) << 16) | ((e + 127) << 23);
  122.    return x;
  123. #else
  124.    return sqrt(x);
  125. #endif
  126. }
  127.  
  128. #endif
  129. /*
  130.  * Initialize tables, etc for fast math functions.
  131.  */
  132. void gl_init_math(void)
  133. {
  134. #ifndef AMIGA
  135.    static GLboolean initialized = GL_FALSE;
  136.  
  137.    if (!initialized) {
  138.       init_sqrt();
  139.  
  140.  
  141.       initialized = GL_TRUE;
  142.    }
  143. #endif
  144. }
  145.